package com.turing.data.scripts.contract.filter

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.TypeReference
import com.turing.common.entity.TextVO
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.common.util.Tools
import com.turing.data.engine.CombineLongText
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils

class Range_30171_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def Regex_PERCENT1 = /.*%.*/
        def Regex_PERCENT2 = /.*比例.*/

        def textPathMap = dependParams.textPathMap
        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()

        try{
            List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)

            if (resultVOs!=null && resultVOs.size()>0) {
                for (int i = 0; i < resultVOs.size(); i++) {
                    if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
//                    return  resultVOs
                        continue
                    }
                    def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def start = resultVOs[i].start
                    def end = resultVOs[i].end

//                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,60)
                    String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,10)

                    if (trueValue =~ Regex_PERCENT1  ||trueValue =~ Regex_PERCENT2){
                        def text = resultVOs[i].text
                        if(!text.contains("%")){
                            def expandOffset = 0
                            while( expandOffset < 10 && start > expandOffset ){
                                def expandString = rawText.substring(end,end+expandOffset)
                                if( expandString.contains("%")  ){
                                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVOs.get(i))
                                    def JSONTextMap = dependParams.JSONTextMap
                                    def fileTextStr = ""
                                    fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                                    StringBuilder cleanText = new StringBuilder();
                                    List<TextVO> textVOS = JSON.parseObject(fileTextStr, new TypeReference<ArrayList<TextVO>>() {});
                                    for (TextVO textVO : textVOS) {
                                        List<TextVO.Line> lines = textVO.getParagraphs().get(0).getLines();
                                        for (TextVO.Line line : lines) {
                                            if(line.getObjType().equals("textLine")){
                                                cleanText.append(line.getObjContent());
                                            }else{
                                                String objContent = line.getObjContent();
                                                JSONArray rows = JSONArray.parseArray(objContent);
                                                for (Object row : rows) {
                                                    JSONObject rowObj = JSONObject.parseObject(JSON.toJSONString(row));
                                                    String rowContent = rowObj.getString("objContent");
                                                    cleanText.append(rowContent);
                                                }
                                            }
                                        }
                                    }
                                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, 0, expandOffset, fileTextStr, cleanText.toString())
                                    trueValueSet.add(objectNew)
                                    return trueValueSet
                                }
                                expandOffset ++
                            }
                        }
                        trueValueSet.add(resultVOs.get(i))
                        return trueValueSet
                    }
                }
            }}catch(Exception e){
            e.printStackTrace()
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = "其他"
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet
    }

}
